<template>
  <div class="comment">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item, index) in list" :key="index">
        <template #title>
          <div class="item">
            <van-image
              width="35"
              height="35"
              :src="item.aut_photo"
              round
              fit="cover"
            />
            <div class="msg">
              <div class="name">{{ item.aut_name }}</div>
              <div class="com">{{ item.content }}</div>
              <div class="other">
                <span class="time">{{ item.pubdate | timefromnow }}</span>
                <van-button @click="replyBtn(item)" size="mini"
                  >回复 {{ item.reply_count }}</van-button
                >
              </div>
            </div>
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>
    <!-- 底部输入框 -->
    <van-field
      @keyup.enter="sendCom()"
      class="myfield"
      v-model="content"
      clearable
      placeholder="请输入评论"
    >
      <template #button>
        <van-button @click="sendCom" size="small" type="info">发布</van-button>
      </template>
    </van-field>

    <van-action-sheet v-model="sheetShow" title="发表回复">
      <reply v-if="sheetShow" :currentCom="currentCom" />
    </van-action-sheet>
  </div>
</template>

<script>
import { getCommentsAPI, setCommentsAPI } from "@/api";
import reply from "./reply.vue";
export default {
  components: {
    reply,
  },
  data() {
    return {
      loading: false,
      finished: false,
      list: [],
      content: "",
      art_id: this.$route.query.id,
      offset: undefined,
      end_id: 0,
      sheetShow: false,
      currentCom: {},
    };
  },

  methods: {
    replyBtn(item) {
      this.sheetShow = true;
      this.currentCom = item;
    },
    async onLoad() {
      const res = await getCommentsAPI({
        type: "a",
        source: this.art_id,
        offset: this.offset,
      });
      this.list = [...this.list, ...res.data.data.results];
      this.offset = res.data.data.last_id;
      this.end_id = res.data.data.end_id;
      this.loading = false;
      if (this.offset === this.end_id) {
        this.finished = true;
      }
    },

    async sendCom() {
      const res = await setCommentsAPI({
        target: this.art_id,
        content: this.content,
      });
      this.$toast.success("评论成功");
      // 更新评论
      this.list.unshift(res.data.data.new_obj);
      this.content = "";
    },
  },
};
</script>

<style lang="less">
.comment {
  margin-bottom: 56px;
  .item {
    display: flex;
    .msg {
      flex: 1;
      margin-left: 20px;
      .name {
        color: #466b9d;
      }
      .other {
        display: flex;
        .time {
          margin-right: 10px;
        }
      }
    }
  }
  .myfield {
    position: fixed;
    bottom: 0;
    left: 0;
  }
}
</style>